package com.itany.shop.controller;

import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserExistException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;
import com.itany.shop.service.UserService;

import java.util.List;
import java.util.Scanner;

/**
 * @author 石小俊
 * 用户访问页面,该页面提供了用户注册与登录功能
 */
public class UserController {

    private Scanner sc = new Scanner(System.in);
    private UserService userService = (UserService) ObjectFactory.getObject("userService");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");

    public static void main(String[] args) {
        new UserController().showMenu();
    }

    public void showMenu() {
        System.out.println("*******************欢迎来到紫金商城*******************");
        System.out.print("请选择服务(1.注册 2.登录 3.商品列表 4.退出):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            case 3:
                findAllProducts();
                break;
            case 4:
                System.out.println("提示:您已经退出!");
                return;
            default:
                System.out.println("错误:您输入的选项有误,请重新输入!");
                showMenu();
                break;
        }
    }

    public void register() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        System.out.print("请输入手机号:");
        String phone = sc.next();
        System.out.print("请输入地址:");
        String address = sc.next();

        try {
            // 调用业务层方法实现注册功能
            userService.register(username, password, phone, address);
            System.out.println("提示:恭喜您,注册成功!");
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:对不起,注册失败,失败原因:" + e.getMessage());
        } catch (UserExistException e) {
            System.out.println("错误:对不起,注册失败,失败原因:" + e.getMessage());
        } catch (Exception e) {
            System.out.println("错误:对不起,注册失败,失败原因:" + e.getMessage());
        }
        showMenu();
    }

    public void login() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        try {
            User user = userService.login(username, password);
            System.out.println("提示:欢迎您," + user.getUsername());
            // 登录成功之后进入商城主页
            new MainController(user).showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("错误:对不起,注册失败,失败原因:" + e.getMessage());
            // 登录失败仍然留在当前页
            showMenu();
        } catch (UserNotExistException e) {
            System.out.println("错误:对不起,注册失败,失败原因:" + e.getMessage());
            showMenu();
        } catch (Exception e) {
            System.out.println("错误:对不起,注册失败,失败原因:服务器内部异常");
            showMenu();
        }

    }

    public void findAllProducts() {
        List<Product> products = productService.findAll();
        if (!products.isEmpty()) {
            System.out.println("---------------------------------------------------");
            System.out.println("商品编号\t\t商品名称\t\t商品单价");
            for (Product product : products) {
                System.out.println(product.getId() + "\t\t" + product.getName() + "\t\t" + product.getPrice());
            }
            System.out.println("---------------------------------------------------");
        }

//        System.out.print("请输入查询第几页数据:");
//        String pageNo = sc.next();
//        System.out.print("请输入查询每页展示几条数据:");
//        String pageSize = sc.next();
//        PageInfo<Product> productPage = productService.findPage(pageNo,pageSize);
//        List<Product> products = productPage.getList();
//        if (!products.isEmpty()) {
//            System.out.println("---------------------------------------------------");
//            System.out.println("商品编号\t\t商品名称\t\t商品单价");
//            for (Product product : products) {
//                System.out.println(product.getId() + "\t\t" + product.getName() + "\t\t" + product.getPrice());
//            }
//            System.out.println("---------------------------------------------------");
//        }
        showMenu();
    }
}
